// pages/Me/detail.js
let app = getApp()
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderNumber: '',
        info: {},
        isService: false,
        refundInfo: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.id) {
            this.setData({
                orderNumber: options.id
            })
        }
        if (options.isService) {
            this.setData({
                isService: options.isService == 1 ? true : false
            })
        }
        this.getInfo()
    },
    //返回首页
    goHome() {
        wx.switchTab({
            url: '/pages/Index/Index',
        })
    },
    //支付
    pay() {
        Controller.pay({
            orderNumber: this.data.orderNumber,
            payType: 1
        }).then(res => {
            let data = res.data.baseData;
            wx.hideLoading()
            wx.requestPayment({
                nonceStr: data.nonceStr,
                package: data.package,
                paySign: data.paySign,
                timeStamp: data.timeStamp,
                signType: data.signType,
                success: (res) => {
                    setTimeout(() => {
                        wx.redirectTo({
                            url: "/pages/Me/Order"
                        })
                    }, 1000)
                    wx.showToast({
                        title: "支付成功",
                        icon: 'none',
                        duration: 1000
                    });
                },
                fail: (res) => {
                    setTimeout(() => {
                        wx.redirectTo({
                            url: "/pages/Me/Order"
                        })
                    }, 1000)
                    wx.showToast({
                        title: "支付失败",
                        icon: 'none',
                        duration: 1000
                    });
                }
            })
        })
    },
    //确认收货
    receiving() {
        var that = this;
        wx.showModal({
            title: '提示',
            content: '是否确认收货?',
            success(res) {
                if (res.confirm) {
                    var data = {
                        orderNumber: that.data.orderNumber
                    }
                    Controller.confirmReceipt(data).then(res => {
                        wx.showToast({
                            title: "收货成功"
                        })
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 1200)
                    })
                }
            }
        })

    },
    //申请售后
    service() {
        wx.redirectTo({
            url: '/pages/service/service?id=' + this.data.orderNumber
        })
    },
    //再次购买
    rebuy(e) {
        wx.switchTab({
            url: '/pages/Types/Types'
        })
    },
    //取消订单
    cancelpay(e) {
        var that = this;
        wx.showModal({
            title: '提示',
            content: '是否取消订单?',
            success(res) {
                if (res.confirm) {
                    var data = {
                        orderNumber: that.data.orderNumber
                    }
                    Controller.ordercancel(data).then(res => {
                        wx.showToast({
                            title: "取消成功"
                        })
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 1200)
                    })
                }
            }
        })
    },
    //删除订单
    delete(e) {
        var that = this;
        wx.showModal({
            title: '提示',
            content: '是否删除订单?',
            success(res) {
                if (res.confirm) {
                    var data = {
                        orderNumber: that.data.orderNumber
                    }
                    Controller.orderdelete(data).then(res => {
                        wx.showToast({
                            title: "删除成功"
                        })
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 1200)
                    })
                }
            }
        })
    },
    //取消售后
    cancelafter(e) {
        var that = this;
        wx.showModal({
            title: '提示',
            content: '是否取消售后?',
            success(res) {
                if (res.confirm) {
                    var data = {
                        orderNumber: that.data.orderNumber
                    }
                    Controller.refundcancel(data).then(res => {
                        wx.showToast({
                            title: "取消成功"
                        })
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 1200)
                    })
                }
            }
        })
    },

    //填写售后信息
    goshipper() {
        wx.redirectTo({
            url: '/pages/service/address?id=' + this.data.orderNumber
        })
    },

    //获取详情
    getInfo() {
        Controller.orderdetail({
                orderNumber: this.data.orderNumber
            }).then(res => {
                res.data.baseData.createTime = util.formatTime_2(res.data.baseData.createTime, '-')
                res.data.baseData.num = 0
                res.data.baseData.itemList.forEach(data => {
                    data.specsList = data.stockSpecs ? data.stockSpecs.split(',') : []
                    res.data.baseData.num += data.amount
                })
                if (res.data.baseData.refundStatus != null) {
                    this.refunddetail()
                }
                this.setData({
                    info: res.data.baseData
                })
            })
            // if (this.data.isService == true) {} else {
            //     Controller.orderdetail({
            //         orderNumber: this.data.orderNumber
            //     }).then(res => {
            //         res.data.baseData.createTime = util.formatTime_2(res.data.baseData.createTime, '-')
            //         res.data.baseData.num = 0
            //         res.data.baseData.itemList.forEach(data => {
            //             data.specsList = data.stockSpecs ? data.stockSpecs.split(',') : []
            //             res.data.baseData.num += data.amount
            //         })
            //         this.setData({
            //             info: res.data.baseData
            //         })
            //     })
            // }
    },
    //获取退款详情
    refunddetail() {
        Controller.refunddetail({ orderNumber: this.data.orderNumber }).then(res => {
            if (res.data.baseData) {
                // if(res.data.baseData.reserveTime){
                // res.data.baseData.downtime=InitTime(res.data.baseData.reserveTime.replace(/-/g, '/'))
                // }
                this.setData({
                    refundInfo: res.data.baseData,
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },


})